﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ControlYSeguimiento.Domain;
using ControlYSeguimiento.Logging;

namespace ControlYSeguimiento_Web_App.Monitoring.UserInfo
{
    public partial class UserInfoDetails : System.Web.UI.Page
    {
        private Customer CurrentCustomer = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["customerLogged"];
            if (cookie != null)
            {
#if DEBUG == false
                CurrentCustomer = Facade.Instance.GetCustomerByName(cookie["customerUserName"]);
#else
                CurrentCustomer = new Customer
                {
                    UserName = "lolo",
                    Surname = "Surname",
                    Password = "123456",
                    Name = "Name",
                    MapZoom = 10,
                    MapCoordinate = new Coordinate("-34.48894618571428", "-58.49734540000001"),
                    Mail = "mail",
                    Id = 1,
                    RefreshMapTimer = "30000",
                    EnableAtFirstUse = false,
                    Enable = true,
                    CustomerPlatform = new Platform { Id = 1 }

                };
#endif
                Panel panelLogBox = (Panel)Master.FindControl("PanelLogBox");
                panelLogBox.Visible = false;
                Panel panelNewUserBox = (Panel)Master.FindControl("PanelNewUserBox");
                panelNewUserBox.Visible = false;              
            }
            else
            {
                Response.Redirect("~/Default.aspx?msg=1");
            }
        }

        protected void btnChangeValues_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    this.CurrentCustomer.Password = TextBoxNewPassword.Text;
                    Facade.Instance.SaveCustomer(this.CurrentCustomer);
                }
                catch (Exception ex)
                {
                    Logger.Instance.Fatal(this, "Excepcion: " + ex.Message);
                    Logger.Instance.Fatal(this, "Stack Trace: " + ex.StackTrace);
                    lblChangeValuesError.Text = "An error occurred when sending the mail. Please check the log";
                    lblChangeValuesError.Visible = true;
                }
                Response.Redirect("~/Monitoring/Monitoring.aspx?msg=1");
            }
        }
    }
}